package com.etc.dao.impl;

import com.etc.dao.StudentCourseDao;
import com.etc.model.StudentCourse;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

public class StudentCourseDaoImpl implements StudentCourseDao {
    private SqlSessionFactory sqlSessionFactory;

    public StudentCourseDaoImpl(SqlSessionFactory sqlSessionFactory) {
        this.sqlSessionFactory = sqlSessionFactory;
    }

    public Integer StudentCourseInsert(List<StudentCourse> studentCourses) {
        SqlSession session =  sqlSessionFactory.openSession();
        Integer result = null;
        for(StudentCourse studentCourse : studentCourses){
            result = session.insert("StudentCourseInsert", studentCourse);
            if (result == 1){
                System.out.println("成功添加数据"+studentCourse.toString());
            }
            session.commit();
        }
        return result;
    }

    public List<StudentCourse> StudentCourseAll() {
        SqlSession session =  sqlSessionFactory.openSession();
        List<StudentCourse> studentCourseAll = session.selectList("StudentCourseAll");
        session.close();
        return studentCourseAll;
    }

    public List<StudentCourse> selectById(String scid) {
        SqlSession session =  sqlSessionFactory.openSession();
        StudentCourse studentCourse = new StudentCourse();
        studentCourse.setScid(scid);
        List<StudentCourse> studentCourses = session.selectList("selectById",studentCourse);
        session.close();
        return studentCourses;
    }

    public List<StudentCourse> scById(String scid) {
        SqlSession session =  sqlSessionFactory.openSession();
        StudentCourse studentCourse = new StudentCourse();
        studentCourse.setScid(scid);
        List<StudentCourse> studentCourses = session.selectList("scById",studentCourse);
        session.close();
        return studentCourses;
    }

    public Integer deleteId(String scid) {
        SqlSession session =  sqlSessionFactory.openSession();
        int deleteById = session.delete("deleteId", scid);
        session.commit();
        session.close();
        return deleteById;
    }
}
